<?php

namespace app\api\model\order;

use app\common\model\order\Order as OrderModel;
use app\common\model\order\OrderReferee as OrderRefereeModel;

/**
 * 推荐订单模型
 */
class OrderReferee extends OrderRefereeModel
{
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'app_id',
		'receipt_time',
	];

	/**
	 * 追加字段
	 * @var array
	 */
	protected $append = [
		'create_time',   // 创建时间
	];

	/**
	 * 关联用户表
	 */
	public function user()
	{
		return $this->belongsTo('app\\common\\model\\user\\User')->field('user_id,nickName,create_time');
	}

	/**
	 * 关联订单主表
	 */
	public function orderMaster()
	{
		return $this->belongsTo('app\\common\\model\\order\\Order')->visible(['order_id','order_no','order_status','order_source']);
	}

	/**
	 * @Exp    创建时间
	 * @Author Mr.Z
	 * @Date   2024-02-21
	 */
	public function getCreateTimeAttr($value, $data){
		return date("Y-m-d H:i:s",$data['receipt_time']);
	}

	/**
	 * @Exp    推荐订单列表
	 * @Author Mr.Z
	 * @Date   2024-02-21
	 * @param  [type]     $user_id  [description]
	 * @param  [type]     $param    [description]
	 * @param  integer    $showtype [description]
	 * @return [type]               [description]
	 */
	public function getList($user_id, $param,$showtype=0)
	{
		$model = $this;
		if (isset($param['start_time']) && $param['start_time']) {
			$start_time = strtotime(date("Y-m-d",$param['start_time']));
			$model = $model->where('receipt_time', '>=', $start_time);
		}
		if (isset($param['end_time']) && $param['end_time']) {
			$end_time = strtotime("+1 day",strtotime(date("Y-m-d",$param['end_time'])));
			$model = $model->where('receipt_time', '<', $end_time);
		}
		if ($user_id>0) {
			$model = $model->where('referee_id', '=', $user_id);
		}
		if($showtype==0){
			$list = $model->with(['user', 'orderMaster'])
			->order(['receipt_time' => 'desc'])->append(['first_order','create_time'])
			->paginate($param);
            if(!$list->isEmpty()) {
                $list = $list->toArray();
                foreach ($list['data'] as &$datum) {
                    if (isset($datum['first_order']) && $datum['first_order']) {
                        continue;
                    }
                    $obj = $this->where(['referee_id' => $user_id])->order(['receipt_time' => 'desc'])->find();
                    if ($obj['order_referee_id'] == $datum['order_referee_id']) {
                        $datum['first_order'] = true;
                    }
                }
            }
		}else{
			$list = $model->order(['receipt_time' => 'desc'])->paginate($param);
		}
		return $list;
	}

	/**
	 * @Exp    获取统计
	 * @Author Mr.Z
	 * @Date   2024-02-21
	 * @param  [type]     $user_id [description]
	 * @param  [type]     $param   [description]
	 * @return [type]              [description]
	 */
	public function getSumResult($user_id, $param, $sum_filed=null)
	{
		$model = $this;
		if (isset($param['start_time']) && $param['start_time']) {
			$start_time = strtotime(date("Y-m-d",$param['start_time']));
			$model = $model->where('receipt_time', '>=', $start_time);
		}
		if (isset($param['end_time']) && $param['end_time']) {
			$end_time = strtotime("+1 day",strtotime(date("Y-m-d",$param['end_time'])));
			$model = $model->where('receipt_time', '<', $end_time);
		}
		if ($user_id>0) {
			$model = $model->where('referee_id', '=', $user_id);
		}
		if($sum_filed){
			$sum_result = $model->sum($sum_filed);
			if(empty($sum_result))$sum_result=0;
			$sum_result=number_format($sum_result,2,'.','');
		}else{
			$sum_result = $model->count();
			if(empty($sum_result))$sum_result=0;
		}
		// dump($model->getlastsql());exit;
		return $sum_result;
	}

	/**
	 * @Exp    获取首单数据
	 * @Author Mr.Z
	 * @Date   2024-02-27
	 * @param  [type]     $user_id [description]
	 * @return [type]              [description]
	 */
	public static function getMyOrder($user_id){
		$static = new static;
		return $static->where('user_id', '=', $user_id)->find();
	}
}
